#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QFileInfoList>
#include <QLabel>
#include <QPropertyAnimation>
#include <QPicture>
#include <QPainter>
#include <QDateTime>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->actionOpen, &QAction::triggered, [this](){
        QString dirPath = QFileDialog::getExistingDirectory(this, "打开", "./");

        if (dirPath.isEmpty()) return;

        // 从目录中获取图片列表
        QDir dir(dirPath);
        dir.setFilter(QDir::Files);
        this->fileList = dir.entryInfoList(this->allow);

        // 如果列表不为空，则渲染第一张图片
        if (this->fileList.size() != 0)
        {
            this->pix.load(this->fileList[this->index].absoluteFilePath());
            this->w = this->pix.width();
            this->h = this->pix.height();

            this->drawPic();
        }


    });

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_nextBtn_clicked()
{
    this->index = ++this->index == this->fileList.size() ? 0 : this->index;
    this->pix.load(this->fileList[this->index].absoluteFilePath());
    this->w = this->pix.width();
    this->h = this->pix.height();

    this->drawPic();
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    if (event->angleDelta().ry() == 120)
    {
        this->w *= 1.2;
        this->h *= 1.2;
    }
    else
    {
        this->w /= 1.2;
        this->h /= 1.2;
    }

    this->drawPic();
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
//    qDebug() << this->width() << this->height();
    ui->label->setGeometry((this->width() - this->w) / 2, (this->height() - this->h) / 2, this->w, this->h);
}


void MainWindow::drawPic()
{
    ui->label->resize(this->w, this->h);
    ui->label->setPixmap(this->pix);
    ui->label->setScaledContents(true);
    ui->label->setGeometry((this->width() - this->w) / 2, (this->height() - this->h) / 2, this->w, this->h);
}

void MainWindow::on_prevBtn_clicked()
{
    this->index = --this->index == -1 ? this->fileList.size() - 1 : this->index;
    this->pix.load(this->fileList[this->index].absoluteFilePath());
    this->w = this->pix.width();
    this->h = this->pix.height();

    this->drawPic();
}

void MainWindow::on_actionDir_triggered()
{
    QString dirPath = QFileDialog::getExistingDirectory(this, "打开文件", ".");

    if (dirPath.isEmpty()) return;

    // 从目录中获取图片列表
    QDir dir(dirPath);
    dir.setFilter(QDir::Files);
    this->fileList = dir.entryInfoList(this->allow);

    // 如果列表不为空，则渲染第一张图片
    if (this->fileList.size() != 0)
    {
        QPicture pic;
        QPainter painter;

        for (QFileInfo item : this->fileList)
        {
            qDebug() << item.absoluteFilePath();
            QFileInfo info(item.absoluteFilePath());
            painter.begin(&pic);
            this->pix.load(item.absoluteFilePath());
            painter.drawPixmap(QRect(0, 0, this->pix.width(), this->pix.height()), this->pix);
            painter.end();

            QString fileName = QString("./uploads/%1.zzz").arg(info.fileName());
            pic.save(fileName);

        }
    }
}
